package com.hl.im.server.infrastrructure.config;

import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.nacos.api.naming.NamingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import org.springframework.stereotype.Component;
import org.springframework.cloud.commons.util.InetUtils;

/**
 * @author huanglin
 * @apiNote
 * @date: 2023/09/09 16:20
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class IMServerRegister implements CommandLineRunner {
    private final NacosServiceManager nacosServiceManager;

    private final WebsocketProperties websocketProperties;


    @Override
    public void run(String... args) throws Exception {
        InetUtils inetUtils = new InetUtils(new InetUtilsProperties());
        NamingService namingService = nacosServiceManager.getNamingService(null);
        namingService.registerInstance("ws", inetUtils.findFirstNonLoopbackAddress().getHostAddress(),websocketProperties.getPort());
        log.info("ws socket 服务nacos注册成功");
    }
}
